#!/usr/bin/perl

use strict;
=pod
# use warnings;

# use all useful and necessary modules and libraries
# use Router::Resource;
# use Plack::Builder;
# use Plack::Request;


# the snippet below is a very cut and dry explanation of how to display something in Plac
# using PSGI interface - it is important to supply the following list elements in the order:
# $status - 200
# $header - list [ 'Content-Type', 'text/html']
# $body - [ '<html><body><h1>this seems to work!</h1></body></html>' ]

my $app = sub {
    my $env = shift;
    return [
        200,
        [ 'Content-Type', 'text/html'],
        [ '<html><body><h1>this seems to work!</h1></body></html>' ],
    ];
};
=cut

use Plack::Response;
use Plack::Builder;
use Plack::Request;
use HTML::Template;

# reusable main navigation string
my $main_nav = '
<ul>
  <li><a href="/you">you</a></li>
  <li><a href="/world">world</a></li>
</ul>
';

# initialize a bunch of reusable stuff
my $template = HTML::Template->new(filename => 'main.tmpl');;
my $main_content;
my $res;
my %template_params;

# define the controller subroutines that will be used to address URIs recognized by Plack
my $app1 = sub {
    my $env = shift;

    my $req = Plack::Request->new($env); # create the reequest object
    my $res = Plack::Response->new(200); # create the response object
    my $req_hash = {}; # initialize the hash to contain information about this object
    my %req_hash = (
        'request method' => $req->method,
        'ip address' => $req->address,
        'request uri' => $req->request_uri,
        );

    $main_content = '
    <p>The table below lists some very important information</p>
    <table>
';
    for my $key ( keys %req_hash ) {
        $main_content .= "<tr><th>$key</th><td>$req_hash{$key}</td></tr>";
    }
    $main_content .= '</table>';

    # define the output parameters
    $template->param(TITLE => "Hello World");
    $template->param(MAIN_CONTENT => $main_content);

    $res = Plack::Response->new(200);
    $res->content_type('text/html');
    $res->body($template->output); # where $template output is the output string

    return $res->finalize;
};

my $app2 = sub {
    my $env = shift; # what does this do?
    my $req = Plack::Request->new($env);

    if ($req->method eq 'GET') {
        $main_content = '
    <h2>Hello You! Here is some Lorem Ipsum for you...</h2>
    <p>
    Please enter some Lorem Ipsum...
    </p>
    <form method="post">
    <div>
    <textarea name="my-lorem" cols="45" rows="5">
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
    </textarea>
    </div>
    <input type="submit" value="send"/>
    </form>
    ';

        # define the output parameters
        $template->param(TITLE => "You and Lorem Ipsum");
        $template->param(MAIN_CONTENT => $main_content);

        $res = Plack::Response->new(200);
        $res->content_type('text/html');
        $res->body($template->output); # where $template output is the output string
    }
    elsif ($req->method eq 'POST') {
        # do POST, process POST hash
        my $single;
        for my $key ($req->body_parameters->keys) {
            $single = $req->body_parameters->get_all($key);
            $main_content .= "<p>$key: $single</p>";
        }
        
        $main_content .= "<p>posted data</p>";

        # define the output parameters
        $template->param(TITLE => "You and Lorem Ipsum");
        $template->param(MAIN_CONTENT => $main_content);

        $res = Plack::Response->new(200);
        $res->content_type('text/html');
        $res->body($template->output); # where $template output is the output string
    }
    else {
        # issue 405 error method not supported
        $main_content = "<p>The $req->method method is not allowed</p>";

        # define the output parameters in a different way from above
        # though the above way may be simpler, seting the below may set the hash to be more reusable
        %template_params = (TITLE => "You and Lorem Ipsum",
                            MAIN_CONTENT => $main_content);
        for my $key (keys %template_params) {
            $template->param($key => $template_params{$key});
        }

        #$template->param(TITLE => "You and Lorem Ipsum");
        #$template->param(MAIN_CONTENT => $main_content);
        #$template->param(MAIN_NAV => $main_nav);

        $res = Plack::Response->new(500);
        $res->content_type('text/html');
        $res->body($template->output); # where $template output is the output string
    }

    return $res->finalize;
};

# using URLMap

builder {
    mount "/world" => $app1;
    mount "/you" => $app2;
};
